﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using Castle.Windsor;
using CommonServiceLocator.WindsorAdapter;
using log4net.Config;
using Microsoft.Practices.ServiceLocation;
using MvcMall.Data.NHibernateMaps;
using MvcMall.WinService.CastleWindsor;
using NHibernate.ByteCode.Castle;
using SharpArch.Data.NHibernate;

namespace MvcMall.WinService
{
    public class Programe
    {
        public static ISessionStorage SessionStorage { get; set; }

        private static void InitializeNHibernateSession()
        {
            NHibernateSession.Init(
                SessionStorage,
                new[] { AppDomain.CurrentDomain.BaseDirectory + "MvcMall.Data.dll" },
                new AutoPersistenceModelGenerator().Generate(),
                AppDomain.CurrentDomain.BaseDirectory + @"NHibernate.config");
        }

        public static void Init()
        {
            XmlConfigurator.Configure();

            SessionStorage = new SimpleSessionStorage();
            IWindsorContainer container = new WindsorContainer();
            ComponentRegistrar.AddComponentsTo(container);
            ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
            NHibernateInitializer.Instance().InitializeNHibernateOnce(InitializeNHibernateSession);
            var proxyFactory = new ProxyFactory();//不能删除
        }

        //[STAThread]
        //static void Main()
        //{
        //    Init();
        //    var serviceToRun = new ServiceBase[]{new Service1()};
        //    ServiceBase.Run(serviceToRun);
        //}

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Init();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Test());
        }
    }
}
